package com.ctshk.sapp.system.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.enums.SupplierStatus;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.system.dto.*;
import com.ctshk.rpc.system.req.*;
import com.ctshk.rpc.system.service.ISysSupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 供应商 前端控制器
 * </p>
 *
 * @author 张志朋
 * @since 2021-02-05
 */
@Api(tags = "供应商")
@RestController
@RequestMapping("/sys/supplier")
public class SysSupplierController extends BaseController {

    @DubboReference
    private ISysSupplierService sysSupplierService;

    @PostMapping("/add/test")
    public Result addTest() {
        return sysSupplierService.addBatchSupplierTest();
    }

    @ApiOperation(value = "新增供应商(仅保存)", notes = "新增供应商(仅保存)")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.ADD, operTitle = "新增供应商(仅保存)", operDesc = "新增供应商(仅保存)")
    @PostMapping("/addForSave")
    public Result addSysSupplierForSave(@RequestBody @Valid SysAddSupplierReq sysAddSupplierReq) {
        int addType = 1;
        return sysSupplierService.addSysSupplier(sysAddSupplierReq, getCurrentUser(getRequest()).getId(), addType);
    }

    @ApiOperation(value = "新增供应商(走审批流程)", notes = "新增供应商(走审批流程)")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.ADD, operTitle = "新增供应商(走审批流程)", operDesc = "新增供应商(走审批流程)")
    @PostMapping("/add")
    public Result addSysSupplier(@RequestBody @Valid SysAddSupplierReq sysAddSupplierReq) {
        int addType = 2;
        return sysSupplierService.addSysSupplier(sysAddSupplierReq, getCurrentUser(getRequest()).getId(), addType);
    }

    @ApiOperation(value = "根据id查询供应商详情", notes = "根据id查询供应商详情")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "供应商详情", operDesc = "供应商详情")
    @GetMapping("/info/{id}")
    public Result<SysSupplierDTO> query(@PathVariable Long id) {
        return sysSupplierService.query(id);
    }

    @ApiOperation(value = "编辑供应商(仅保存)", notes = "编辑供应商(仅保存)")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "编辑供应商(仅保存)", operDesc = "编辑供应商(仅保存)")
    @PostMapping("/editForSave")
    public Result editForSave(@RequestBody @Valid SysAddSupplierReq sysAddSupplierReq) {
        int addType = 1;
        return sysSupplierService.editSupplier(sysAddSupplierReq, getCurrentUser(getRequest()).getId(), addType);
    }

    @ApiOperation(value = "编辑供应商(走审批流程)", notes = "编辑供应商(走审批流程)")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "编辑供应商(走审批流程)", operDesc = "编辑供应商(走审批流程)")
    @PostMapping("/editForApproval")
    public Result editForApproval(@RequestBody @Valid SysAddSupplierReq sysAddSupplierReq) {
        int addType = 2;
        return sysSupplierService.editSupplier(sysAddSupplierReq, getCurrentUser(getRequest()).getId(), addType);
    }

    @ApiOperation(value = "供应商列表查询", notes = "供应商列表查询")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "供应商列表查询", operDesc = "供应商列表查询")
    @PostMapping("/querySupplier")
    public Result<PageResponse<SysSupplierQueryListDTO>> queryList(@RequestBody SysSupplierQueryListReq req) {
        PageResponse<SysSupplierQueryListDTO> supplier = sysSupplierService.queryList(req, getCurrentUser());
        return Result.success(supplier);
    }

    @ApiOperation(value = "供应商拉选列表查询(cost_item_id)", notes = "供应商拉选列表查询(cost_item_id)")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY)
    @PostMapping("/queryListByItemId/{id}")
    public Result<List<SysSupplierQueryListDTO>> queryListByItemId(@PathVariable Long id) {
        return sysSupplierService.queryListByItemId(id);
    }

    @ApiOperation(value = "通过名称查询供应商和代理商", notes = "通过名称查询供应商和代理商")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "name", dataType = "String", required = true, value = "供应商或代理商名称")
    })
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "通过名称查询供应商和代理商", operDesc = "通过名称查询供应商和代理商")
    @GetMapping("/querySupplierAndAgent")
    public Result<List<QuerySupplierAndAgentDTO>> querySupplierAndAgent(@RequestParam(value = "name") String name) {
        return sysSupplierService.querySupplierAndAgent(name);
    }

    @ApiOperation(value = "通过名称查询供应商", notes = "通过名称查询供应商")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "name", dataType = "String", required = true, value = "供应商名称"),
            @ApiImplicitParam(paramType = "query", name = "costItemId", dataType = "String", required = true, value = "供应商成本项id")
    })
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "通过名称查询供应商", operDesc = "通过名称查询供应商")
    @GetMapping("/querySupplierByName")
    public Result<List<QuerySupplierByNameDTO>> querySupplierByName(@RequestParam(value = "name", required = true) String name, @RequestParam(value = "costItemId", required = true) String costItemId) {
        return sysSupplierService.querySupplierByName(name, costItemId);
    }

    @ApiOperation(value = "搜索供应商", notes = "搜索供应商")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "搜索供应商", operDesc = "搜索供应商")
    @PostMapping("/searchByName")
    public Result<PageResponse<SearchSupplierDTO>> searchByName(@RequestBody SearchSupplierReq req) {
        return sysSupplierService.searchByName(req);
    }

    @ApiOperation(value = "搜索供应商All", notes = "搜索供应商All")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "搜索供应商", operDesc = "搜索供应商")
    @PostMapping("/searchByNameAll")
    public Result<PageResponse<SearchSupplierDTO>> searchByNameAll(@RequestBody SearchSupplierReq req) {
        return sysSupplierService.searchByNameAll(req);
    }

    @ApiOperation(value = "供应商审核", notes = "供应商审核")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "供应商审核", operDesc = "供应商审核")
    @PostMapping("/approval")
    public Result approval(@RequestBody @Valid SysSupplierApprovalReq req) {
        req.setUserId(getCurrentUser(getRequest()).getId());
        return sysSupplierService.approval(req);
    }

    @ApiOperation(value = "修改供应商状态", notes = "修改供应商状态")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "修改供应商状态", operDesc = "修改供应商状态")
    @PostMapping("/updateStatus")
    public Result updateStatus(@RequestBody @Valid SysSupplierUpdateStatusReq req) {
        req.setUserId(getCurrentUser(getRequest()).getId());
        return sysSupplierService.updateStatus(req);
    }

    @ApiOperation(value = "删除供应商", notes = "删除供应商")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY)
    @GetMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id) {
        return sysSupplierService.deleteById(id);
    }
}
